package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.models.dto.CommodityCategoryDTO;
import com.ztgf.commodity.models.service.ICommodityCategoryService;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgSectionMapper;
import com.ztgf.shopping.models.bo.epg.EpgSectionBO;
import com.ztgf.shopping.models.bo.epg.EpgSectionSaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.*;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgSectionService;
import com.ztgf.shopping.models.vo.epg.EpgSectionVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;

/**
 * 频道栏目服务
 *
 * @author YUE
 * @create 2020-05-19 14:38
 */
@DubboService
public class EpgSectionServiceImpl extends ServiceImpl<EpgSectionMapper, EpgSectionDTO> implements IEpgSectionService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgSectionMapper epgSectionMapper;

    @DubboReference
    private ICommodityInfoService commodityInfoService;

    @Autowired
    private EpgSectionCommodityRelationServiceImpl epgSectionCommodityRelationService;

    @DubboReference
    private ICommodityCategoryService commodityCategoryService;

    @Override
    public boolean add(EpgSectionSaveBO epgSectionSaveBO) {
        EpgSectionDTO epgSectionDTO = new EpgSectionDTO();
        BeanUtils.copyProperties(epgSectionSaveBO, epgSectionDTO);
        Long id = ymlConfig.getPkId();
        epgSectionDTO.setId(id);
        return this.insert(epgSectionDTO);
    }

    public Long addAndReturnId(EpgSectionSaveBO epgSectionSaveBO) {
        EpgSectionDTO epgSectionDTO = new EpgSectionDTO();
        BeanUtils.copyProperties(epgSectionSaveBO, epgSectionDTO);
        Long id = ymlConfig.getPkId();
        epgSectionDTO.setId(id);
        insert(epgSectionDTO);
        return id;
    }

    /**
     * 删除功能功能相关的频道栏目
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgSectionMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件选择查询 频道栏目 有页码
     * @param epgSectionBO
     * @return
     */
    @Override
    public Page<EpgSectionDTO> getBySelectSectionInfoPage(EpgSectionBO epgSectionBO){
        Wrapper<EpgSectionDTO> wrapper = getBySelectSectionInfo(epgSectionBO);
        return selectPage(new Page<>(epgSectionBO.getPageNo(),epgSectionBO.getPageSize()),wrapper);
    }

    /**
     * 根据条件选择查询 频道栏目 无页码
     * @param epgSectionBO
     * @return
     */
    @Override
    public List<EpgSectionDTO> getBySelectSectionInfoNoPage(EpgSectionBO epgSectionBO){
        epgSectionBO = addMustData(epgSectionBO);
        Wrapper<EpgSectionDTO> wrapper = getBySelectSectionInfo(epgSectionBO);
        if(epgSectionBO.getPageNo() == 0 && epgSectionBO.getPageSize() == 0)
            return epgSectionMapper.selectPage( new Page<>(0, ShoppingConstant.PLATFORM_SECTION),wrapper);
        return selectList(wrapper);
    }

    /**
     * 加上类目的频道信息
     * @param epgSectionBO
     * @return
     */
    @Override
    public List<EpgSectionVO> getAndCategory(EpgSectionBO epgSectionBO){
        List<EpgSectionDTO> epgSectionDTOList = getBySelectSectionInfoNoPage(epgSectionBO);
        List<EpgSectionVO> epgSectionVOList = new ArrayList<>();
        for (EpgSectionDTO epgSectionDTO : epgSectionDTOList) {
            EpgSectionVO epgSectionVO = new EpgSectionVO();
            BeanUtils.copyProperties(epgSectionDTO, epgSectionVO);
            String firstCategory = epgSectionDTO.getFirstCategory();
            Long zeroCategory = epgSectionDTO.getZeroCategory();
            //一级类目
            if(StrUtils.isNotEmpty(firstCategory)){
                List<String> firstCategoryIdList = Arrays.asList(firstCategory.split(","));
                List<CommodityCategoryDTO> commodityCategoryDTOS = commodityCategoryService.selectBatchIds(firstCategoryIdList);
                epgSectionVO.setCommodityCategoryDTOList(commodityCategoryDTOS);
            }
            //零级类目
            if(null != zeroCategory){
                CommodityCategoryDTO commodityCategoryDTO = commodityCategoryService.selectById(epgSectionDTO.getZeroCategory());
                epgSectionVO.setZeroCommodityCategoryDTO(commodityCategoryDTO);
            }
            epgSectionVOList.add(epgSectionVO);
        }
        return epgSectionVOList;
    }

    /**
     * 添加必要数据
     * @param epgSectionBO
     * @return
     */
    private EpgSectionBO addMustData(EpgSectionBO epgSectionBO){
        epgSectionBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgSectionBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgSectionBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgSectionBO;
    }

    public Wrapper<EpgSectionDTO> getBySelectSectionInfo(EpgSectionBO epgSectionBO){
        Wrapper<EpgSectionDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("status", epgSectionBO.getStatus());
        wrapper.eq("is_publish", epgSectionBO.getIsPublish());
        wrapper.eq("module_id", epgSectionBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgSectionBO.getSort())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}